import type { ResponseData, ResponsePaginationData } from '@/utils/request';
import { SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import type { AddItem, AdminAccount, DetailItem, EditItem, QueryListParams } from './data';
import type { AuthRole } from '@/pages/Auth/AuthRole/data';
import { request } from 'umi';
import { message } from 'antd';

export { queryAdminRoleList } from '@/pages/Auth/AuthRole/service';

type Pagination = { current?: number; pageSize?: number };

/**
 * 获取管理员列表
 * @param params
 */
export async function queryAdminList(
  params: Pagination & QueryListParams,
): Promise<RequestData<AdminAccount>> {
  const { current = 1, pageSize = 20, name, userName } = params;
  try {
    const res: ResponsePaginationData<AdminAccount> = await request('/sys/admin/list', {
      params: { page: current, pageSize, name, userName },
      method: 'GET',
    });

    if (res?.code !== SUCCESS_CODE) {
      return { success: false, total: 0, data: [] };
    }

    return {
      success: res.code === SUCCESS_CODE,
      total: res.data.total,
      data: res.data.list,
    };
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

export async function queryAdminDetail(params: {
  id: string;
}): Promise<{ success: boolean; data?: DetailItem }> {
  try {
    const res = await request(`/sys/admin/${params.id}`, { method: 'GET' });

    if (res?.code !== SUCCESS_CODE) {
      return { success: false };
    }
    return {
      success: true,
      data: res.data,
    };
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { success: false, data: undefined };
  }
}

/**
 * 创建一个管理员
 * @param params
 */
export async function addAdmin(params: AddItem) {
  const { name, userName, password } = params;
  try {
    const res = await request('/sys/admin/', {
      method: 'POST',
      data: { name, userName, pwd: password },
    });
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

// 修改一个管理员
export async function editAdmin(params: EditItem) {
  const { id, name } = params;
  try {
    const res = await request(`/sys/admin/${id}`, { method: 'POST', data: { name } });
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

export interface UpdateAdminRoleParams {
  id: string;
  roles?: AuthRole[];
}

export interface UpdateAdminBaseInfoParams {
  id: string;
  name?: string;
}

// 修改管理员角色
export async function updateAdminInfo(
  params: UpdateAdminBaseInfoParams,
): Promise<ResponseData<boolean>> {
  try {
    const res = await request(`/sys/admin/update_info`, {
      data: { admin_id: params.id, name: params.name },
      method: 'POST',
    });
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return {
      code: 1,
      data: false,
      success: false,
      message: '服务器请求失败，请重试，如果多次请求失败，请联系管理员',
    };
  }
}

// 修改管理员角色
export async function updateAdminRoles(
  params: UpdateAdminRoleParams,
): Promise<ResponseData<boolean>> {
  const { id, roles } = params;
  const roleIds = roles?.map((item) => item.id);

  try {
    const res = await request(`/sys/admin/${id}/roles`, { data: { roleIds }, method: 'POST' });
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return {
      code: 1,
      data: false,
      success: false,
      message: '服务器请求失败，请重试，如果多次请求失败，请联系管理员',
    };
  }
}

// 删除
export async function removeAdmin(params: { id: string }) {
  try {
    const res = await request(`/sys/admin/delete`, {
      method: 'POST',
      params: {
        admin_id: params.id,
      },
    });
    if (res.code === SUCCESS_CODE) {
      return res;
    }
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

// 重置密码
export async function resetAdmin(params: { id?: string }) {
  const { id } = params;
  try {
    const res = await request(`/sys/admin/${id}/reset_user_password`, { method: 'POST' });
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}

//修改管理员的密码
export async function updateAdminPassword(params: { password?: string; admin_id?: string }) {
  try {
    const res = await request(`/sys/admin/reset_user_password`, {
      method: 'POST',
      data: { ...params },
    });
    return res;
  } catch (error) {
    message.error('服务器请求失败，请重试，如果多次请求失败，请联系管理员');
    return { code: 1, data: [] };
  }
}
